package com.example.stuwork.controller;

import com.example.stuwork.common.Result;
import com.example.stuwork.dao.AssignmentAllocationStats;
import com.example.stuwork.pojo.AssignmentAllocation;
import com.example.stuwork.service.AssignmentAllocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作业分配控制器
 */
@RestController
@RequestMapping("/assignment-allocations")
public class AssignmentAllocationController {
    
    @Autowired
    private AssignmentAllocationService assignmentAllocationService;
    
    /**
     * 根据ID获取作业分配记录
     */
    @GetMapping("/{id}")
    public Result<AssignmentAllocation> getAllocationById(@PathVariable("id") Long allocationId) {
        return assignmentAllocationService.getAllocationById(allocationId);
    }
    
    /**
     * 根据作业ID获取作业分配记录列表
     */
    @GetMapping("/assignment/{assignmentId}")
    public Result<List<AssignmentAllocation>> getAllocationsByAssignmentId(@PathVariable("assignmentId") Long assignmentId) {
        return assignmentAllocationService.getAllocationsByAssignmentId(assignmentId);
    }
    
    /**
     * 根据学生ID获取作业分配记录列表
     */
    @GetMapping("/student/{studentId}")
    public Result<List<AssignmentAllocation>> getAllocationsByStudentId(@PathVariable("studentId") String studentId) {
        return assignmentAllocationService.getAllocationsByStudentId(studentId);
    }
    
    /**
     * 根据作业ID和学生ID获取作业分配记录
     */
    @GetMapping("/assignment/{assignmentId}/student/{studentId}")
    public Result<AssignmentAllocation> getAllocationByAssignmentIdAndStudentId(
            @PathVariable("assignmentId") Long assignmentId,
            @PathVariable("studentId") String studentId) {
        return assignmentAllocationService.getAllocationByAssignmentIdAndStudentId(assignmentId, studentId);
    }
    
    /**
     * 根据作业ID和状态获取作业分配记录列表
     */
    @GetMapping("/assignment/{assignmentId}/status/{status}")
    public Result<List<AssignmentAllocation>> getAllocationsByAssignmentIdAndStatus(
            @PathVariable("assignmentId") Long assignmentId,
            @PathVariable("status") Integer status) {
        return assignmentAllocationService.getAllocationsByAssignmentIdAndStatus(assignmentId, status);
    }
    
    /**
     * 添加作业分配记录
     */
    @PostMapping("")
    public Result<AssignmentAllocation> addAllocation(@RequestBody AssignmentAllocation allocation) {
        return assignmentAllocationService.addAllocation(allocation);
    }
    
    /**
     * 批量添加作业分配记录
     */
    @PostMapping("/batch")
    public Result<Boolean> batchAddAllocations(@RequestBody List<AssignmentAllocation> allocations) {
        return assignmentAllocationService.batchAddAllocations(allocations);
    }
    
    /**
     * 更新作业分配记录
     */
    @PutMapping("/{id}")
    public Result<AssignmentAllocation> updateAllocation(
            @PathVariable("id") Long allocationId,
            @RequestBody AssignmentAllocation allocation) {
        allocation.setAllocationId(allocationId);
        return assignmentAllocationService.updateAllocation(allocation);
    }
    
    /**
     * 更新作业分配记录状态
     */
    @PutMapping("/{id}/status/{status}")
    public Result<Boolean> updateAllocationStatus(
            @PathVariable("id") Long allocationId,
            @PathVariable("status") Integer status) {
        return assignmentAllocationService.updateAllocationStatus(allocationId, status);
    }
    
    /**
     * 更新作业提交ID
     */
    @PutMapping("/{id}/submission/{submissionId}/status/{status}")
    public Result<Boolean> updateSubmissionId(
            @PathVariable("id") Long allocationId,
            @PathVariable("submissionId") Long submissionId,
            @PathVariable("status") Integer status) {
        return assignmentAllocationService.updateSubmissionId(allocationId, submissionId, status);
    }
    
    /**
     * 更新作业分配记录评分
     */
    @PutMapping("/{id}/score/{score}")
    public Result<Boolean> updateScore(
            @PathVariable("id") Long allocationId,
            @PathVariable("score") Integer score) {
        return assignmentAllocationService.updateScore(allocationId, score);
    }
    
    /**
     * 删除作业分配记录
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteAllocation(@PathVariable("id") Long allocationId) {
        return assignmentAllocationService.deleteAllocation(allocationId);
    }
    
    /**
     * 根据作业ID删除所有相关的作业分配记录
     */
    @DeleteMapping("/assignment/{assignmentId}")
    public Result<Boolean> deleteAllocationsByAssignmentId(@PathVariable("assignmentId") Long assignmentId) {
        return assignmentAllocationService.deleteAllocationsByAssignmentId(assignmentId);
    }
    
    /**
     * 获取作业提交统计信息
     */
    @GetMapping("/assignment/{assignmentId}/stats")
    public Result<AssignmentAllocationStats> getAssignmentStats(@PathVariable("assignmentId") Long assignmentId) {
        return assignmentAllocationService.getAssignmentStats(assignmentId);
    }
    
    /**
     * 同步课程作业给学生，按需分配之前的课程作业
     * 当学生访问课程详情页时调用此接口，检查并同步该课程的作业
     */
    @GetMapping("/sync/student/{studentId}/class/{classId}")
    public Result<Boolean> syncAssignmentsForStudent(
            @PathVariable("studentId") String studentId,
            @PathVariable("classId") Long classId) {
        return assignmentAllocationService.syncAssignmentsForNewStudent(studentId, classId);
    }
} 